package com.lzh.seatrend.common.entity;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname Page
 * @Description 分页基类
 * @Date 2021/8/24 11:03
 * @Created by liuzihao
 */
public class Page<T> implements Serializable {

    /**
     * 当前页码
     */
    private int curPage = 1;

    /**
     * 页面大小，设置为"-1"表示 不进行分页
     */
    private int pageSize = 10;

    /**
     * 总记录数,设置为"-1"表示不查询总数
     */
    private long count;

    /**
     * 首页索引
     */
    private int first;

    /**
     * 尾页索引
     */
    private int last;

    /**
     * 上一页索引
     */
    private int prev;

    /**
     * 下一页索引
     */
    private int next;

    /**
     * 是否是第一页
     */
    private boolean IsfirstPage;

    /**
     * 是否是最后一页
     */
    private boolean IsLastPage;

    private List<T> list = new ArrayList<>();

    /**
     * 排序属性
     * orderField: 排序字段
     * orderDirection: 排序方式 desc,asc
     */
    private String orderField = "";
    private String orderDirection = "";

    public Page() {
        this.pageSize = -1;
    }

    public Page(int curPage, int pageSize) {
        this(curPage,pageSize,0);
    }

    public Page(int curPage, int pageSize, long count) {
        this(curPage,pageSize,count,new ArrayList<>());
    }

    public Page(int curPage, int pageSize, long count, List<T> list) {
        this.setCount(count);
        this.setCurPage(curPage);
        this.setPageSize(pageSize);
        this.list = list;
    }

    /**
     * @param response
     * @Description
     * @Param request
     * @date 2021/8/24 11:32
     * @auther liuzihao
     */
    public Page(HttpServletRequest request, HttpServletResponse response) {
        //设置分页参数
        String curPage = request.getParameter("curPage");
        if (StringUtils.isNumeric(curPage)) {
            this.setCurPage(Integer.parseInt(curPage));
        }
        String pageSize = request.getParameter("pageSize");
        if (StringUtils.isNumeric(pageSize)) {
            this.setPageSize(Integer.parseInt(pageSize));
        }
        //设置排序参数
        String orderField = request.getParameter("orderField");
        if (StringUtils.isNotBlank(orderField)) {
            this.setOrderField(orderField);
        }
        String orderDirection = request.getParameter("orderDirection");
        if (StringUtils.isNotBlank(orderDirection)) {
            this.setOrderDirection(orderDirection);
        }
    }

    /**
     * @return com.lzh.springsecurity.common.entity.Page<T>
     * @Description 设置本页数据对象列表
     * @Param list
     * @date 2021/8/24 13:32
     * @auther liuzihao
     */
    public Page<T> setList(List<T> list) {
        this.list = list;
        initialize();
        return this;
    }

    /**
     * @return void
     * @Description 设置数据总数
     * @Param count
     * @date 2021/8/24 13:50
     * @auther liuzihao
     */
    public void setCount(long count) {
        this.count = count;
        if (pageSize >= count) {
            curPage = 1;
        }
    }

    /**
     * 初始化参数
     */
    public void initialize() {
        this.first = 1;
        this.last = (int) (count / (this.pageSize < 1 ? 20 : this.pageSize) + first - 1);
        if (this.count % this.pageSize != 0 || this.last == 0) {
            this.last++;
        }

        if (this.last < this.first) {
            this.last = this.first;
        }

        if (this.curPage <= 1) {
            this.curPage = this.first;
        }

        if (this.curPage >= this.last) {
            this.curPage = this.last;
            this.IsLastPage = true;
        }

        if (this.curPage < this.last - 1) {
            this.next = this.curPage + 1;
        } else {
            this.next = this.last;
        }

        if (this.curPage > 1) {
            this.prev = this.curPage - 1;
        } else {
            this.prev = this.first;
        }

        //如果当前页小于首页
        if (this.curPage < this.first) {
            this.curPage = this.first;
        }
        //如果当前页大于尾页
        if (this.curPage > this.last) {
            this.curPage = this.last;
        }
    }


    public static void main(String[] args) {
//        String test = "1";
//        int test2 = 1;
//        System.out.println(StringUtils.isNumeric(test));
//        System.out.println(StringUtils.isNumeric(test2));

        String test = "a   b ";
        System.out.println(StringUtils.isNotBlank(test));
    }

    public int getCurPage() {
        return curPage;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    /**
     * 设置页面大小(最大500)
     * @param pageSize
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize <= 0 ? 10 : pageSize;
    }

    public long getCount() {
        return count;
    }

    public void setCountOld(long count) {
        this.count = count;
    }

    public int getFirst() {
        return first;
    }

    public void setFirst(int first) {
        this.first = first;
    }

    public int getLast() {
        return last;
    }

    public void setLast(int last) {
        this.last = last;
    }

    public int getPrev() {
        return prev;
    }

    public void setPrev(int prev) {
        this.prev = prev;
    }

    public int getNext() {
        return next;
    }

    public void setNext(int next) {
        this.next = next;
    }

    public boolean isIsfirstPage() {
        return IsfirstPage;
    }

    public void setIsfirstPage(boolean isfirstPage) {
        IsfirstPage = isfirstPage;
    }

    public boolean getLastPage() {
        return IsLastPage;
    }

    public void setLastPage(boolean lastPage) {
        IsLastPage = lastPage;
    }

    public List<T> getList() {
        return list;
    }

    public void setListOld(List<T> list) {
        this.list = list;
    }

    public String getOrderField() {
        return orderField;
    }

    public void setOrderField(String orderField) {
        this.orderField = orderField;
    }

    public String getOrderDirection() {
        return orderDirection;
    }

    public void setOrderDirection(String orderDirection) {
        this.orderDirection = orderDirection;
    }
}
